package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.soms.dto.CaClassMemberDto;
import com.dingreading.cloud.soms.entity.CaClassMember;
import com.dingreading.cloud.soms.entity.table.CaClassMemberTableDef;
import com.dingreading.cloud.soms.mapper.CaClassMemberMapper;
import com.dingreading.cloud.soms.service.CaClassMemberService;
import com.dingreading.cloud.soms.util.ScheduleUtil;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.dingreading.cloud.soms.entity.table.CaClassTableDef.caClass;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CaClassMemberServiceImpl extends BaseServiceImpl<CaClassMemberMapper, CaClassMember, CaClassMemberTableDef> implements CaClassMemberService {

    @Override
    protected CaClassMemberTableDef getTable() {
        return CaClassMemberTableDef.caClassMember;
    }

    @Resource
    private CaClassMemberMapper classMemberMapper;

    @Override
    public List<CaClassMemberDto> getClassMemberList(String corpUid, String storeUid, Long classId, String projectUid, String keyword) {
        return classMemberMapper.getClassMemberList(corpUid, storeUid, classId, projectUid, ScheduleUtil.TIYAN_UID, keyword);
    }

    @Override
    public List<String> getClassMemberUid(String storeUid, Long classId) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.classId.eq(classId));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.memberUid)
                .from(table)
                .where(condition);

        return getMapper().selectObjectListByQueryAs(wrapper, String.class);

    }

    @Override
    public int dropMember(String storeUid, Long classId, String memberUid) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.classId.eq(classId))
                .and(table.memberUid.eq(memberUid));

        return getMapper().deleteByCondition(condition);
    }

    @Override
    public long getClassMemberNum(String storeUid, Long classId) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.classId.eq(classId));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.count(table.id))
                .from(table)
                .where(condition);

        return getMapper().selectCountByQuery(wrapper);

    }

    @Override
    public String getByClassIdAndMemberUid(String storeUid, Long classId, String memberUid) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.classId.eq(classId))
                .and(table.memberUid.eq(memberUid));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.memberUid)
                .from(table)
                .where(condition);

        return getMapper().selectObjectByQueryAs(wrapper, String.class);
    }

    @Override
    public boolean haveMember(String storeUid, Long classId, String memberUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.classId.eq(classId))
                .and(table.memberUid.eq(memberUid));
        return exists(condition);
    }

    @Override
    public List<String> getClassNamesByMemberUid(String storeUid, String memberUid) {
//        QueryWrapper wrapper = QueryWrapper.create()
//                .select(CaClass::getClassName)
//                .from(CaClassMember.class).as("a")
//                .leftJoin(CaClass.class).as("b")
//                .on(queryWrapper -> queryWrapper.where(CaClassMember::getClassId).eq(CaClass::getId))
//                .where(o -> {
//                    o.eq(CaClassMember::getStoreUid, storeUid)
//                            .eq(CaClassMember::getMemberUid, memberUid)
//                            .eq(CaClass::getCloseStatus, 1);
//                });
        QueryWrapper wrapper = QueryWrapper.create()
                .select(caClass.className)
                .from(table)
                .leftJoin(caClass).on(table.classId.eq(caClass.id))
                .where(table.storeUid.eq(storeUid)
                        .and(table.memberUid.eq(memberUid))
                        .and(caClass.closeStatus.eq(1)));
        return getMapper().selectObjectListByQueryAs(wrapper, String.class);
    }

}
